﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Auction
{
    public partial class FAddPackageToAuction : Form
    {
        private EDM_AuctionContainer context;
        Package packageFound = null;
        Validations valid = new Validations();
        private Auction auction;

        public FAddPackageToAuction(EDM_AuctionContainer context, Auction auction)
        {
            this.context = context;
            this.auction = auction;
            InitializeComponent();
        }


        private void loadDataGrid()
        {
            int x = 0;
            if (auction != null)
            {
                if (auction.Package != null)
                {
                    foreach (WorkOfArt aWork in packageFound.WorkOfArt)
                    {
                        dG_Packages.Rows.Add();
                        dG_Packages.Rows[x].Cells[Author.Index].Value = aWork.AuthorName;
                        dG_Packages.Rows[x].Cells[id_Package.Index].Value = aWork.Package.idPackage;
                        dG_Packages.Rows[x].Cells[id.Index].Value = aWork.idWorkOfArt;
                        dG_Packages.Rows[x].Cells[nameOfWork.Index].Value = aWork.nameOfWork;
                        dG_Packages.Rows[x].Cells[author_surname.Index].Value = aWork.AuthorSurname;
                        dG_Packages.Rows[x].Cells[year.Index].Value = aWork.year;
                        x++;
                    }
                }
            }
            if (x == 0)
            {
                l_NoWorks.Visible = true;
                l_list.Visible = false;
            }
        }

        private void rdbSearchById_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbSearchById.Checked)
            {
                grb_ID.Enabled = true;
                b_SearchPackage.Enabled = true;
            }
            else
            {
                grb_ID.Enabled = false;
            }
        }

        private void rdbSearchByPackageName_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbSearchByPackageName.Checked)
            {
                grb_Name.Enabled = true;
                b_SearchPackage.Enabled = true;
            }
            else
            {
                grb_Name.Enabled = false;
            }
        }

        private void b_SearchPackage_Click(object sender, EventArgs e)
        {
            lblAdvNotFound.Visible = false;
            packageFound = null;
            if (rdbSearchById.Checked)
            {
                int id = valid.GetInt(t_id);
                try
                {
                    packageFound = context.PackageSet.FirstOrDefault(p => p.idPackage == id);
                    foreach (Auction anAuction in context.AuctionSet)
                    {
                        foreach (Package aPackage in anAuction.Package)
                        {
                            if (aPackage.idPackage == id)
                            {
                                packageFound = null;
                                lblAdvNotFound.Visible = false;
                                l_alreadyAssigned.Visible = true;
                            }
                        }
                    }
                }
                catch (System.Exception)
                {
                    packageFound = null;
                    lblAdvNotFound.Visible = true;
                }

            }
            else if (rdbSearchByPackageName.Checked)
            {
                try
                {
                    packageFound = context.PackageSet.FirstOrDefault(p => p.nameOfPackage == t_Name.Text.ToString());
                }
                catch (System.Exception)
                {
                    packageFound = null;
                }
            }

            if (packageFound != null)
            {
                TransferPackageFound(packageFound);
            }
            else
            {
               // lblAdvNotFound.Visible = true;
            }
        }

        public void TransferPackageFound(Package aPackage)
        {
            this.packageFound = aPackage;
            //b_save.Visible = true;
            gB_DataPackages.Visible = true;
            gB_Search.Visible = true;
            LoadDataOfPackage();
        }

        private void LoadDataOfPackage()
        {
            if (this.packageFound != null)
            {

                t_minimun.Text = packageFound.minimunSellingPrice.ToString();
                t_Name.Text = packageFound.nameOfPackage;
                t_Sold.Text = packageFound.sold.ToString();
                t_outputPrice.Text = packageFound.outputPrice.ToString();
                t_description.Text = packageFound.description;
                b_save.Visible = true;
                loadDataGrid();
            }
        }

        private void b_save_Click(object sender, EventArgs e)
        {
            if (packageFound.WorkOfArt.Count != 0)
            {
                auction.Package.Add(packageFound);
                context.SaveChanges();
            }
            else
            {
                MessageBox.Show("You can't add a package without works", "Advice", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
}
